// JavaScript Document
  /**
  * define validate structure
  */
  function ValidateResult(len){
  	this.resultArr = new Array(len) ;
  	this.length    = len;
  	this.current   = 0;
  }
  ValidateResult.prototype.add=function(Result,Severity,Reason,NodeIdStr){
  		var result = new resultItem(Result,Severity,Reason,NodeIdStr);
  		this.resultArr[this.current++] = result;
  }
  ValidateResult.prototype.updateTree = function(tree){
	for(i = 0; i < this.length; i++){
  		var currObj = document.getElementById(this.resultArr[i].nodeIdStr);
  		var treeNode = tree._get_node ( currObj );
  		if("Error" == this.resultArr[i].severity){
  			treeNode.children("a").css("color","red");
  		}else{
  			treeNode.children("a").css("color","#FFCF29");
  		}
  		treeNode.children("a").css("font-weight","bold");
  		treeNode.parents(".jstree-closed").each(function () {
            $("#treeContent").jstree("open_node", this, false, true);
    	});
	}   
 }
 ValidateResult.prototype.clean = function(tree){
     for(i = 0; i < this.length; i++){
 		var currObj = document.getElementById(this.resultArr[i].nodeIdStr);
  		var treeNode = tree._get_node ( currObj );
  		treeNode.children("a").css("color","");
  		treeNode.children("a").css("font-weight","");
     }
 }
 function resultItem(Result,Severity,Reason,NodeIdStr){
	  	this.result    = Result;
	  	this.severity  = Severity;
	  	this.reason    = Reason;
	  	this.nodeIdStr = NodeIdStr;
 }
  
 function cleanValidate(tree){
	  	var xmlData = ajaxValidateXml;
	  	var len = $(xmlData).find("length").text();
	  	var validateObj = new ValidateResult(len);
	  	var Result;
	  	var Reason;
	  	var NodeIdStr;
	 	$(xmlData).find("validateResult").each(function(index, element) {
	        Result      = $(this).children("Result").text();
	        Severity    = $(this).children("Severity").text();
	        Reason      = $(this).children("Reason").text();
			NodeIdStr   = $(this).children("NodeIdStr").text();
	    	validateObj.add(Result,Severity,Reason,NodeIdStr);
	    });
	    validateObj.clean(tree);
	    hideStateNotice();//关闭错误提示框
 }
 /**************************************************************************************
 ***************** 文件保存部分 Begin ***************************************************/
 function saveModuleToDb(){
 	// Ajax Validate Module 
	/*-------------function begin---------------------------*/	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "ValidateModuleForSaveServlet",
			//提交的数�
			data: {},
			beforeSend: function(XMLHttpRequest){
            },
            success: function(data, textStatus){
                 var result    =  $(data).find("result").text();
                 var resultMsg =  $(data).find("resultMsg").text();
                 if(result != "true"){
                 	if(resultMsg == "sessionOut"){
                 		  $("#validateNotice").html("Session 过期了，请重新登录！");
                 		  $("#validateOption").html("<a href='login.jsp' target='_top' /a> 界面");
                 	}
                 	showValidateForSave(data);
                 }else{
                    submitSaveModule();
                 }
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				//请求出错处理
				//$.jstree.rollback(data.rlbk); 
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function submitSaveModule(trigger){
 	/*-------------function begin---------------------------*/	
 	var noticeStr = "【保存】";
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "SaveModuleToDBServlet",
			//提交的数�
			data: {},
			beforeSend: function(XMLHttpRequest){
			    if(trigger == "autoSave"){
			    	noticeStr ="【自动保存】 ";
			    	$("#NoticeContent").html("自动保存中……<br/>");
                 	showStateNotice();
			    }
				delaySaveModule();
            },
            success: function(data, textStatus){
            	 var result    =  $(data).find("result").text();
                 var resultMsg =  $(data).find("resultMsg").text();
                 if(result == "success"){
                 	$("#NoticeContent").html(noticeStr+"操作成功！<br/>");
                 	showStateNotice();
                 	setTimeout(function(){hideStateNotice();},3000);
                 }
		    },
			complete: function(XMLHttpRequest, textStatus){
				//delaySaveModule();
			},
			error: function(){
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function delaySaveModule(){
 	hideValidateForSave();
 }
  
 
 function getPrev_node(parentObj,targetObj){
 	var tree     = jQuery.jstree._reference('#treeContent');
	var prev_node  = tree._get_prev(targetObj,false);
	if(prev_node.attr("id") == parentObj.attr("id")){
		return prev_node;
	}
	var parentNode = tree._get_parent(prev_node);
	if(parentNode.attr("id") == parentObj.attr("id")){
		return prev_node;
	}
	return findPrevBrother(tree,parentObj,parentNode);
 }
 function findPrevBrother(tree,parentObj,targetObj){
 	var parentNode = tree._get_parent(targetObj);
 	if(parentNode.attr("id") == parentObj.attr("id")){
		return targetObj;
	}else{
		return findPrevBrother(tree,parentObj,parentNode);
	}
 }
  